﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dune2013
{

    public class IrCosecharBehavior : Behavior
    {
        public IrCosecharBehavior() :
            base(){
                typeBehavior = TypeBehavior.IrA;
            }
        Node pozoActual;
        public override void execute(int dTime,Actor act)
        {

            base.execute(dTime,act);

            if (pozoActual == null || act.wayPoints == null)
            {
                int xPos, yPos;
                Map mapa = Game1.getInstance().map;
                xPos = (int)act.position.X / mapa.NODE_WIDTH;
                yPos = (int)act.position.Y / mapa.NODE_HEIGHT;
                pozoActual = mapa.findClosestPOI(xPos,yPos);
                if (pozoActual == null) {
                    ((Recolector)act).noHayMasPozos = true;
                    return;
                }
                act.setWaypoints(mapa.findPath(xPos, yPos, (int)pozoActual.mapPosition.X, (int)pozoActual.mapPosition.Y,false));
            }

            if (act.wayPoints.Count == 0) {
                act.wayPoints = null;
                switch (pozoActual.type) { 
                    case Node.NODE_TYPE.POI: //Aun sin construir
                        act.changeBehavior(new ConstruirBehavior(pozoActual, StaticStuff.tiempoTotalConstruccionPozo));
                        break;
                    case Node.NODE_TYPE.WELL: //Ya construido
                        act.changeBehavior(new RecolectarBehavior(pozoActual,StaticStuff.velocidadExtraccionPetroleo, StaticStuff.velocidadGastoNafta));
                        break;
                    case Node.NODE_TYPE.NOTHING: //Esta vacio, no deberia tener sentido que llegue a este punto...
                        act.changeBehavior(new IrCosecharBehavior());
                        break;
                    default:
                        throw new Exception("Como llegué acá? Nodo de tipo: " + pozoActual.type);
                }
            }

        }

        public override void enter(Actor act)
        {
            base.enter(act); 
            //Map mapa = Game1.getInstance().map;
            //act.setWaypoints(Game1.getInstance().map.findPath((int)act.position.X / mapa.NODE_WIDTH, (int)act.position.Y / mapa.NODE_HEIGHT, 10, 10));
        }

        public override void exit(Actor act)
        {
            base.exit(act);
        }
    }
}
